package de.koller.worlders.util;

import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.oceanFramework.util.MIn;
import de.koller.oceanFramework.util.MOut;
import de.koller.worlders.engine.tiles.data.MapData;
import de.koller.worlders.engine.tiles.data.TileKey;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import javax.imageio.ImageIO;

public class PatternToMaps {

	private boolean useClearColor = false;
	private int clearColor;
	
	private final BufferedImage input;
	private final BufferedImage[][] output;
	private Map<Integer, Integer> replacer = null;
		
	public PatternToMaps( String input ) throws Exception {
		this.input = ImageIO.read( new File( input ) );
		this.output = new BufferedImage[ this.input.getWidth() / MapData.MAP_SIZE ]
									   [ this.input.getHeight() / MapData.MAP_SIZE ];
		for( int x = 0; x < output.length; x++ )
			for( int y = 0; y < output[0].length; y++ )
				output[x][y] = new BufferedImage( MapData.MAP_SIZE, MapData.MAP_SIZE, BufferedImage.TYPE_INT_ARGB );
	}
	
	public void generate() {
		for( int six = 0; six < output.length; six++ )
			for( int siy = 0; siy < output[0].length; siy++ )
				for( int x = 0; x < MapData.MAP_SIZE; x++ )
					for( int y = 0; y < MapData.MAP_SIZE; y++ )
						setData( six, siy, x, y );
	}

	public void setClearColor( int clearColor ) {
		this.clearColor = clearColor;
		this.useClearColor = true;
	}
	
	private void setData( int six, int siy, int x, int y ) {
		int ix = six * MapData.MAP_SIZE + x;
		int iy = siy * MapData.MAP_SIZE + y;
		
		int d = input.getRGB( ix, iy );
		
		if( useClearColor )
			d = clearColor;
		else if( replacer != null ) 
			if( replacer.containsKey( d ) )
				d = replacer.get( d );
		
		output[six][siy].setRGB( x, y, d );
	}
	
	public void save( String file, String prefix, String postfix ) throws Exception {
		for( int x = 0; x < output.length; x++ )
			for( int y = 0; y < output[0].length; y++ )
				ImageIO.write( output[x][y], "png", new File( file + prefix + 
							( (x==0&&y==0) ? "" : TileKey.toKey( MapData.MAP_SIZE*x, MapData.MAP_SIZE*y ) ) + 
							postfix + ".png" ) );
	}
	
	public void createReplacerData( String file ) {
		File f = new File( file );
		if( f.exists() )
			return;
		
		Set<Integer> colors = new HashSet<>();
		for( int x = 0; x < input.getWidth(); x++ )
			for( int y = 0; y < input.getHeight(); y++ )
				colors.add( input.getRGB( x, y ) );
		
		MOut o = new MOut( file );
		for( Integer c : colors )
			o.writeString( " " + Integer.toHexString( c )  + " ff000000\n" );
		
		o.close();
	}
	
	public void loadReplacerData( String file ) {
		replacer = new HashMap<>();
		
		MIn in = new MIn( file );
		String i = in.readString( 1 )[0];
		while( !i.isEmpty() ) {
			StringTokenizer st = new StringTokenizer( i );
			replacer.put( getInt( st.nextToken() ), 
						  getInt( st.nextToken() ) );
			i = in.readString( 1 )[0];
		}
	}
	
	private int getInt( String hex ) {
		return ColorIntUtil.getIntColor( 
				Integer.parseInt( hex.substring( 0, 2 ), 16 ),
				Integer.parseInt( hex.substring( 2, 4 ), 16 ),
				Integer.parseInt( hex.substring( 4, 6 ), 16 ),
				Integer.parseInt( hex.substring( 6, 8 ), 16 ) 
			);
	}
	
	public static void main( String[] args ) throws Exception {
	
		PatternToMaps light = new PatternToMaps( "./resources/level/patternDraw.png" );
		light.setClearColor( 0xff000000 );
		light.generate();
		light.save( "./resources/level/", "default", "_lightMap" );
		
		PatternToMaps draw = new PatternToMaps( "./resources/level/patternDraw.png" );
		draw.generate();
		draw.save( "./resources/level/", "default", "_draw" );
		
		PatternToMaps map = new PatternToMaps( "./resources/level/patternMap.png" );
		map.createReplacerData( "./resources/level/mapReplace.txt" );
		map.loadReplacerData( "./resources/level/mapReplace.txt" );
		map.generate();
		map.save( "./resources/level/", "default", "_map" );
	}
	
	
}
